import React, { useState, useEffect, useRef } from 'react';
import { createModel } from 'hox';
import { message } from 'antd';
import { deviceAssociationProcess, getEnabledProcess, getFlowAll, startFlow } from '../service.js';
import { useRequest } from '@umijs/hooks';

function useFlowModel() {
  const [flowList, setFlowList] = useState([]);
  //执行流程
  const { run: executeProcess } = useRequest(startFlow, {
    manual: true,
    onSuccess: (res, params) => {
      if (res.result) {
        message.success('流程执行完成！');
      } else {
        message.error(res.msg);
      }
    },
  });
  //设备关联流程
  const { run: deviceContext } = useRequest(deviceAssociationProcess, {
    manual: true,
    onSuccess: (res, params) => {
      if (res.result) {
        message.success('保存成功!');
      } else {
        message.error(res.msg);
      }
    },
  });
  //设备关联流程
  const { run: associatedProcess } = useRequest(getEnabledProcess, {
    manual: true,
  });
  //获取全部流程
  const { run: getFlowList } = useRequest(getFlowAll, {
    manual: true,
    onSuccess: async (res, params) => {
      if (res.result) {
        setFlowList(res.data || []);
      } else {
        message.error(res.msg);
      }
    },
  });
  return {
    getFlowList,
    flowList,
    executeProcess,
    associatedProcess,
    deviceContext,
  };
}

export default createModel(useFlowModel);
